package com.anzia.properties;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Properties;
import java.util.Set;

/**
 *@ClassName Properties_Demo
 *@Description
 *@Author Anzia.
 *@Date 2021/4/9 20:23
 *@Version 1.0
 **/
public class Properties_Demo {
    public static void main(String[] args) throws Exception {
        //创建集合
        Properties properties = new Properties();

        //添加数据
        properties.setProperty("username","zhangsan");
        properties.setProperty("age","20");
        System.out.println(properties.toString());

        //遍历
        // keySet
        // entrySet
        // stringPropertyName()
        Set<String> propertyNames = properties.stringPropertyNames();
        for (String propertyName : propertyNames) {
            System.out.println(propertyName + "\t" + properties.getProperty(propertyName));
        }

        //和流有关的方法
        // list方法
        //PrintWriter pw = new PrintWriter("static\\print.txt");
        //properties.list(pw);
        //pw.close();

        // store方法
        //FileOutputStream fos = new FileOutputStream("static//store.properties");
        //properties.store(fos,"注释");
        //fos.close();

        // load方法
        Properties properties1 = new Properties();
        FileInputStream fis = new FileInputStream("static\\store.properties");
        properties1.load(fis);
        fis.close();
        System.out.println(properties1.toString());
    }
}
